package com.EAIPClub.UserAuthDataManager.service.impl;

import com.EAIPClub.UserAuthDataManager.mapper.UserAuthDataManagerMapper;
import com.EAIPClub.UserAuthDataManager.service.UserAuthDataManagerService;
import com.api.ConstantPool;
import com.api.POJO.UserAuthDataManager.AllUserAuthDataReturnMsg;
import com.api.POJO.UserAuthDataManager.UserAuthData;
import com.api.POJO.UserAuthDataManager.UserAuthDataManagerReturnMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description :用户登录信息业务逻辑层实现
 * @Author :崔昌赫
 * @Date : 2021/10/20
 */
@Service
@Slf4j
public class UserAuthDataManagerServiceImpl implements UserAuthDataManagerService {

    private UserAuthDataManagerMapper mapper;
    @Autowired
    public void setMapper(UserAuthDataManagerMapper mapper) {
        this.mapper = mapper;
    }
    private UserAuthData inspectIsNull(UserAuthData userAuthData,UserAuthDataManagerReturnMsg oldUserAuthData){
        if(userAuthData.getUid()==null){
            log.info("检测到uid为空,将保留原uid.uid:{}",oldUserAuthData.getAuthData().getUid());
            userAuthData.setUid(oldUserAuthData.getAuthData().getUid());
        }
        if(userAuthData.getToken()==null){
            log.info("检测到token为空,将保留原token.token:{}",oldUserAuthData.getAuthData().getToken());
            userAuthData.setToken(oldUserAuthData.getAuthData().getToken());
        }
        if(userAuthData.getDate()==null){
            log.info("检测到date为空,将保留原date.date:{}",oldUserAuthData.getAuthData().getDate());
            userAuthData.setDate(oldUserAuthData.getAuthData().getDate());
        }
        if(userAuthData.getExpiration()==null){
            log.info("检测到expiration为空,将保留原expiration.expiration:{}",oldUserAuthData.getAuthData().getExpiration());
            userAuthData.setExpiration(oldUserAuthData.getAuthData().getExpiration());
        }
        return userAuthData;
    }

    @Override
    public AllUserAuthDataReturnMsg getAllUserAuthData() {
        AllUserAuthDataReturnMsg allUserAuthDataReturnMsg = new AllUserAuthDataReturnMsg();
        List<UserAuthData> allUserAuthData = mapper.getAllUserAuthData();
        List<String> Msg = new ArrayList<>();
        if(allUserAuthData==null){
            allUserAuthDataReturnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("获取所有登录信息失败!");
            allUserAuthDataReturnMsg.setMsg(Msg);
        }else {
            allUserAuthDataReturnMsg.setNum(allUserAuthData.size());
            allUserAuthDataReturnMsg.setUserAuthData(allUserAuthData);
        }
        return allUserAuthDataReturnMsg;
    }

    @Override
    public UserAuthDataManagerReturnMsg addNewUserAuthData(UserAuthData authData) {
        UserAuthDataManagerReturnMsg returnMsg = new UserAuthDataManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将增加新的用户登录信息,用户登录信息:{}",authData);
        if(!mapper.addNewUserAuthData(authData)){
            log.warn("增加新的用户登录信息失败!用户登录信息:{}",authData);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("添加用户登录信息失败!");
        }
        log.info("增加新的用户登录信息成功!用户登录信息:{}",authData);
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public UserAuthDataManagerReturnMsg getUserAuthDataByUid(String uid) {
        UserAuthDataManagerReturnMsg returnMsg = new UserAuthDataManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将通过uid获取用户登录信息,uid:{}",uid);
        UserAuthData userAuthDataByUid = mapper.getUserAuthDataByUid(uid);
        if(userAuthDataByUid==null){
            log.warn("通过uid获取用户登录信息失败!uid:{}",uid);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("通过uid获取用户登录信息失败!");
        }
        log.info("通过uid获取用户登录信息成功!用户登录信息:{},uid:{}",userAuthDataByUid,uid);
        returnMsg.setMsg(Msg);
        returnMsg.setAuthData(userAuthDataByUid);
        return returnMsg;
    }

    @Override
    public UserAuthDataManagerReturnMsg getUserAuthDataByToken(String token) {
        UserAuthDataManagerReturnMsg returnMsg = new UserAuthDataManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将通过token获取用户登录信息,token:{}",token);
        UserAuthData userAuthDataByToken = mapper.getUserAuthDataByToken(token);
        if(userAuthDataByToken==null){
            log.warn("通过token获取用户登录信息失败!token:{}",token);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("通过token获取用户登录信息失败!");
        }
        log.info("通过token获取用户登录信息成功!用户登录信息:{},token:{}",userAuthDataByToken,token);
        returnMsg.setMsg(Msg);
        returnMsg.setAuthData(userAuthDataByToken);
        return returnMsg;
    }

    @Override
    public UserAuthDataManagerReturnMsg getUserAuthDataByDate(String date) {
        UserAuthDataManagerReturnMsg returnMsg = new UserAuthDataManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将通过date获取用户登录信息,date:{}",date);
        List<UserAuthData> userAuthDataByDate = mapper.getUserAuthDataByDate(date);
        if(userAuthDataByDate==null){
            log.warn("通过date获取用户登录信息失败!date:{}",date);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("通过date获取用户登录信息失败!");
        }
        log.info("通过token获取用户登录信息成功!用户登录信息:{},date:{}",userAuthDataByDate,date);
        returnMsg.setMsg(Msg);
        returnMsg.setAuthDataList(userAuthDataByDate);
        return returnMsg;
    }

    @Override
    public UserAuthDataManagerReturnMsg getUserAuthDataByExpiration(String expiration) {
        UserAuthDataManagerReturnMsg returnMsg = new UserAuthDataManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将通过expiration获取用户登录信息,expiration:{}",expiration);
        List<UserAuthData> userAuthDataByExpiration = mapper.getUserAuthDataByExpiration(expiration);
        if(userAuthDataByExpiration==null){
            log.warn("通过expiration获取用户登录信息失败!expiration:{}",expiration);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("通过expiration获取用户登录信息失败!");
        }
        log.info("通过token获取用户登录信息成功!用户登录信息:{},expiration:{}",userAuthDataByExpiration,expiration);
        returnMsg.setMsg(Msg);
        returnMsg.setAuthDataList(userAuthDataByExpiration);
        return returnMsg;
    }

    @Override
    public UserAuthDataManagerReturnMsg updateUserAuthDataByUid(String uid, UserAuthData userAuthData) {
        UserAuthDataManagerReturnMsg returnMsg = new UserAuthDataManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将通过uid更新用户登录信息,uid:{},新用户登录信息:{}",uid,userAuthData);
        UserAuthDataManagerReturnMsg userAuthDataByUid = getUserAuthDataByUid(uid);
        if(userAuthDataByUid==null){
            log.warn("通过uid查找用户信息失败，停止更新用户信息！uid:{}",uid);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("获取用户登录信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        if(userAuthDataByUid.getStatus()!=200){
            log.warn("通过uid查找用户信息为空，停止更新用户信息！uid:{},返回信息:{}",uid,userAuthDataByUid.getMsg());
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("获取用户登录信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        UserAuthData authData = inspectIsNull(userAuthData, userAuthDataByUid);
        if(!mapper.updateUserAuthDataByUid(uid,authData)){
            log.warn("通过uid更新用户登录信息失败!用户登录信息:{}",authData);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("通过uid更新用户登录信息失败!");
        }
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public UserAuthDataManagerReturnMsg updateUserAuthDataByToken(String token, UserAuthData userAuthData) {
        UserAuthDataManagerReturnMsg returnMsg = new UserAuthDataManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将通过token更新用户登录信息,token:{},新用户登录信息:{}",token,userAuthData);
        UserAuthDataManagerReturnMsg userAuthDataByToken = getUserAuthDataByToken(token);
        if(userAuthDataByToken==null){
            log.warn("通过token查找用户信息失败，停止更新用户信息！token:{}",token);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("获取用户登录信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        if(userAuthDataByToken.getStatus()!=200){
            log.warn("通过token查找用户信息为空，停止更新用户信息！token:{},返回信息:{}",token,userAuthDataByToken.getMsg());
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("获取用户登录信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        UserAuthData authData = inspectIsNull(userAuthData, userAuthDataByToken);
        if(!mapper.updateUserAuthDataByToken(token,authData)){
            log.warn("通过token更新用户登录信息失败!用户登录信息:{}",authData);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("通过token更新用户登录信息失败!");
        }
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public UserAuthDataManagerReturnMsg updateUserAuthDataByDate(String date, UserAuthData userAuthData) {
        UserAuthDataManagerReturnMsg returnMsg = new UserAuthDataManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将通过date更新用户登录信息,date:{},新用户登录信息:{}",date,userAuthData);
        UserAuthDataManagerReturnMsg userAuthDataByDate = getUserAuthDataByDate(date);
        if(userAuthDataByDate==null){
            log.warn("通过date查找用户信息失败，停止更新用户信息！date:{}",date);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("获取用户登录信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        if(userAuthDataByDate.getStatus()!=200){
            log.warn("通过date查找用户信息为空，停止更新用户信息！date:{},返回信息:{}",date,userAuthDataByDate.getMsg());
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("获取用户登录信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        UserAuthData authData = inspectIsNull(userAuthData, userAuthDataByDate);
        if(!mapper.updateUserAuthDataByDate(date,authData)){
            log.warn("通过date更新用户登录信息失败!用户登录信息:{}",authData);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("通过date更新用户登录信息失败!");
        }
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public UserAuthDataManagerReturnMsg updateUserAuthDataByExpiration(String expiration, UserAuthData userAuthData) {
        UserAuthDataManagerReturnMsg returnMsg = new UserAuthDataManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将通过expiration更新用户登录信息,expiration:{},新用户登录信息:{}",expiration,userAuthData);
        UserAuthDataManagerReturnMsg userAuthDataByExpiration = getUserAuthDataByExpiration(expiration);
        if(userAuthDataByExpiration==null){
            log.warn("通过expiration查找用户信息失败，停止更新用户信息！expiration:{}",expiration);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("获取用户登录信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        if(userAuthDataByExpiration.getStatus()!=200){
            log.warn("通过expiration查找用户信息为空，停止更新用户信息！expiration:{},返回信息:{}",expiration,userAuthDataByExpiration.getMsg());
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("获取用户登录信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        UserAuthData authData = inspectIsNull(userAuthData, userAuthDataByExpiration);
        if(!mapper.updateUserAuthDataByExpiration(expiration,authData)){
            log.warn("通过expiration更新用户登录信息失败!用户登录信息:{}",authData);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("通过expiration更新用户登录信息失败!");
        }
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public UserAuthDataManagerReturnMsg deleteUserAuthDataByUid(String uid) {
        UserAuthDataManagerReturnMsg returnMsg = new UserAuthDataManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将通过uid删除用户登录信息,uid:{}",uid);
        UserAuthDataManagerReturnMsg userAuthDataByUid = getUserAuthDataByUid(uid);
        if(userAuthDataByUid.getStatus()!=200){
            log.warn("通过uid查找用户信息为空，停止更新用户信息！uid:{},返回信息:{}",uid,userAuthDataByUid.getMsg());
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("获取用户登录信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        if(!mapper.deleteUserAuthDataByUid(uid)){
            log.warn("通过uid删除用户登录信息失败!uid:{}",uid);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("通过uid删除用户登录信息失败!");
        }
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public UserAuthDataManagerReturnMsg deleteUserAuthDataByToken(String token) {
        UserAuthDataManagerReturnMsg returnMsg = new UserAuthDataManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将通过token删除用户登录信息,token:{}",token);
        UserAuthDataManagerReturnMsg userAuthDataByToken = getUserAuthDataByToken(token);
        if(userAuthDataByToken.getStatus()!=200){
            log.warn("通过token查找用户信息为空，停止更新用户信息！token:{},返回信息:{}",token,userAuthDataByToken.getMsg());
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("获取用户登录信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        if(!mapper.deleteUserAuthDataByToken(token)){
            log.warn("通过token删除用户登录信息失败!token:{}",token);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("通过token删除用户登录信息失败!");
        }
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public UserAuthDataManagerReturnMsg deleteUserAuthDataByDate(String date) {
        UserAuthDataManagerReturnMsg returnMsg = new UserAuthDataManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将通过date删除用户登录信息,date:{}",date);
        UserAuthDataManagerReturnMsg userAuthDataByDate = getUserAuthDataByDate(date);
        if(userAuthDataByDate.getStatus()!=200){
            log.warn("通过date查找用户信息为空，停止更新用户信息！date:{},返回信息:{}",date,userAuthDataByDate.getMsg());
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("获取用户登录信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        if(!mapper.deleteUserAuthDataByDate(date)){
            log.warn("通过date删除用户登录信息失败!date:{}",date);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("通过date删除用户登录信息失败!");
        }
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    @Override
    public UserAuthDataManagerReturnMsg deleteUserAuthDataByExpiration(String expiration) {
        UserAuthDataManagerReturnMsg returnMsg = new UserAuthDataManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将通过expiration删除用户登录信息,expiration:{}",expiration);
        UserAuthDataManagerReturnMsg userAuthDataByExpiration = getUserAuthDataByExpiration(expiration);
        if(userAuthDataByExpiration.getStatus()!=200){
            log.warn("通过expiration查找用户信息为空，停止更新用户信息！expiration:{},返回信息:{}",expiration,userAuthDataByExpiration.getMsg());
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("获取用户登录信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        if(!mapper.deleteUserAuthDataByExpiration(expiration)){
            log.warn("通过expiration删除用户登录信息失败!expiration:{}",expiration);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("通过expiration删除用户登录信息失败!");
        }
        returnMsg.setMsg(Msg);
        return returnMsg;
    }
}
